package com.situ.blog.controller;

import com.situ.blog.pojo.vo.BlogTypeCountVO;
import com.situ.blog.service.IBlogService;
import com.situ.blog.service.IBlogTypeService;
import com.situ.blog.service.impl.BlogServiceImpl;
import com.situ.blog.service.impl.BlogTypeServiceImpl;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/echarts")
public class EChartsServlet extends HttpServlet {
    private IBlogTypeService blogTypeService = new BlogTypeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "getEChartsPage":
                getEChartsPage(req, resp);
                break;
            case "selectBlogTypeCount":
                selectBlogTypeCount(req, resp);
                break;
        }
    }

    private void getEChartsPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/views/echarts.jsp").forward(req, resp);
    }

    private void selectBlogTypeCount(HttpServletRequest req, HttpServletResponse resp) {
        List<BlogTypeCountVO> list = blogTypeService.selectBlogTypeCount();
        JSONUtil.toJSON(resp, Result.ok(list));
    }
}
